package com.pro.common.modules.service.dependencies.config.component;

import com.pro.framework.api.enums.ThirdErrorType;
import com.pro.framework.api.intf.ThirdExceptionClassifier;
import com.pro.framework.api.model.IFrameworkException;
import com.pro.framework.api.model.ThirdException;
import com.pro.framework.api.util.ExceptionUtil;
import org.springframework.stereotype.Component;

@Component
public class DefaultThirdExceptionClassifier implements ThirdExceptionClassifier {

    @Override
    public ThirdException classify(Throwable e) {

        // 1. 是否可重试（网络原因）
        if (ExceptionUtil.isRetryableException(e)) {
            return new ThirdException("default", ThirdErrorType.RETRYABLE,
                    ExceptionUtil.logThrowable(e), e);
        }

        // 2. 是否框架定义的异常
        if (e instanceof IFrameworkException fe) {
            return new ThirdException("default", ThirdErrorType.BUSINESS, ExceptionUtil.logThrowable((Throwable) fe), (Throwable) fe);
        }

        // 3. 最终兜底
        return new ThirdException("default", ThirdErrorType.UNKNOWN,
                ExceptionUtil.logThrowable(e), e);
    }

    @Override
    public String getPlatformName() {
        return "default";
    }
}
